package com.kedacom.freedomLabel.base;

import com.kedacom.freedomLabel.Options;
import com.kedacom.freedomLabel.model.bean.Template;
import com.kedacom.freedomLabel.utils.FileIOUtils;
import com.kedacom.lego.message.LegoMessageManager;
import com.seray.scaleviewlib.utils.CacheDiskUtils;

import java.io.File;

/**
 * @author licheng
 * @since 2019/12/23 14:36
 */
public class BasicCache {

    private static final String LOCAL_FILE_NAME = BasicCache.class.getSimpleName();
    private static BasicCache sBasicCache;
    private static CacheDiskUtils sCacheDiskUtils;

    public static BasicCache getInstance() {
        if (sBasicCache == null) {
            synchronized (BasicCache.class) {
                if (sBasicCache == null) {
                    sBasicCache = new BasicCache();
                }
            }
        }
        return sBasicCache;
    }

    private BasicCache() {
        String cacheDirectoryPath = FileIOUtils.getCacheDirectoryPath();
        FileIOUtils.createOrExistsDir(new File(cacheDirectoryPath));
        sCacheDiskUtils = CacheDiskUtils.getInstance(cacheDirectoryPath, LOCAL_FILE_NAME);
    }

    public void saveTemplate(Template template) {
        sCacheDiskUtils.put("template", template);
        LegoMessageManager.getInstance().sendMessage(Options.MSG_UPDATE_TEMPLATE, template);
    }

    public Template getTemplate() {
        return sCacheDiskUtils.getParcelable("template", Template.CREATOR);
    }

    public void savePrinterItemIndex(String key, int dataIndex) {
        sCacheDiskUtils.put(key, dataIndex);
        LegoMessageManager.getInstance().sendMessage(key, dataIndex);
    }

    public int getPrinterItemIndex(String key) {
        return (int) sCacheDiskUtils.getSerializable(key, -1);
    }
}